//
//  CommunityListViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CommunityListViewController.h"
#import "EditTimelineViewController.h"
#import "CommunityTableTableViewController.h"
#import "HHCoinViewController.h"

@interface CommunityListViewController ()<TopChannelContianerViewDelegate,UIScrollViewDelegate,SendTimeLineDoneDelegate>



@end

@implementation CommunityListViewController

#pragma mark - Get

-(UIBarButtonItem *)rightBtnItem
{
    if (!_rightBtnItem) {
        _rightBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addMore"] style:UIBarButtonItemStylePlain target:self action:@selector(processAddMore)];
    }
    return _rightBtnItem;
}
-(ChooseComTypeView *)chooseTypeView
{
    if (!_chooseTypeView) {
        _chooseTypeView = [[ChooseComTypeView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, 44)];
        _chooseTypeView.delegate = self;

    }
    return _chooseTypeView;
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        CGFloat height = KScreen_Height - KNaviheight - KTabBarHeight - 44;
        _scrollView.frame = CGRectMake(0, 44, KScreen_Width, height);
        _scrollView.contentSize = CGSizeMake(KScreen_Width * self.channelsArray.count, height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initWithUserInterface];
}

-(void)initWithUserInterface
{
    self.navigationItem.rightBarButtonItem = self.rightBtnItem;
    
    self.channelsArray = @[KNSLocal(@"护肤"),KNSLocal(@"美妆"),KNSLocal(@"生活"),
                           KNSLocal(@"服饰"),KNSLocal(@"食品"),KNSLocal(@"保健"),
                           KNSLocal(@"其他")];
    
    [self.view addSubview:self.chooseTypeView];
    
    [self.view addSubview:self.scrollView];
    
    [self setupChildController];
    
    self.chooseTypeView.channelNameArray = self.channelsArray;
}

#pragma mark --private Method--初始化子控制器
-(void)setupChildController
{
    self.tableViewArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.channelsArray.count; i++) {
        CommunityTableTableViewController * vc = [[CommunityTableTableViewController alloc] init];
        vc.dataType = i;
        vc.view.frame = CGRectMake(KScreen_Width * i, 0,KScreen_Width, self.scrollView.frame.size.height);
        
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        
        [self.tableViewArray addObject:vc];
    }
}

#pragma mark -  chooseView delegate
- (void)chooseChannelWithIndex:(NSInteger)index
{
    self.currentIndex = index;
    
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * self.currentIndex, 0) animated:YES];
    
    id vc = self.tableViewArray[self.currentIndex];
    [vc beginLoadingData];
    
    [self canProcessNext];
}

#pragma mark --UIScrollViewDelegate-- 滑动的减速动画结束后会调用这个方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        self.currentIndex = scrollView.contentOffset.x / self.scrollView.frame.size.width;
        [self.chooseTypeView didSelectChannelButtonWithIndex:self.currentIndex];
    }
}

#pragma mark -  朋友圈发布新数据 刷新界面 delegate
-(void)sendTimeLineContentDoneWithIndex:(NSInteger)index
{
    CommunityTableTableViewController * vc = self.tableViewArray[index];
    [vc headerRefreshData];
    //展示一次广告
    [self canProcessNext];
}


#pragma mark - 点击更多
-(void)processAddMore
{
    if (![self canProcessNext]) {
        return;
    }
    if ([UserInfoManger shared].contact.coins < [NSNumber numberWithInteger:10] && ![UserInfoManger shared].isRemovedAD) {
        [self systemAlertWithTitle:nil andMessage:KNSLocal(@"积分不足") actionTextOne:KNSLocal(@"alertCancel") actionTextTwo:KNSLocal(@"alertOK") actionOneBlock:^{
            
        } actionTwoBlock:^{
            HHCoinViewController *vc = [[HHCoinViewController alloc]initWithNibName:@"HHCoinViewController" bundle:nil];
            [self presentViewController:vc animated:true completion:nil];
        }];
        return;
    }
    
    EditTimelineViewController * editVC = [[EditTimelineViewController alloc] init];
    editVC.delegate = self;
    editVC.timeLineType = TimeLineType_dynamic;
    editVC.dataType = self.currentIndex;
    [self.navigationController pushViewController:editVC animated:YES];
}




@end
